/**
 * @author 黄波
 * 创建日期：2011Sep 6, 2011
 * TODO// N/A
 */
package com.platform.manager.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.platform.manager.bo.PowerModuleBo;
import com.platform.manager.bo.UserGroupPowerBo;
import com.platform.pojo.GroupPower;
import com.platform.pojo.PowerModule;

//权限<->模块对应关系配置类
public class ProPowerModuleAction extends ManagerSupport {

	private String powerId;
	private String[] chk;	//权限ID组
	/**
	 * @return the powerId
	 */
	public String getPowerId() {
		return powerId;
	}
	/**
	 * @param powerId the powerId to set
	 */
	public void setPowerId(String powerId) {
		this.powerId = powerId;
	}
	/**
	 * @return the chk
	 */
	public String[] getChk() {
		return chk;
	}
	/**
	 * @param chk the chk to set
	 */
	public void setChk(String[] chk) {
		this.chk = chk;
	}
	@Override
	public String execute() throws Exception {
		String msg = null;
//		chks = ServletActionContext.getRequest().getParameterValues("chk");
		if(powerId == null || powerId.equals("")) {
			msg = "请选择权限！";
		} else if (chk == null || chk.length == 0) {
			msg = "请选择模块！";
		} else {
			//取得该用户组的所有权限
			PowerModuleBo bo = new PowerModuleBo();
			List<PowerModule> modules = bo.getModules(powerId);
			if(chk.length > modules.size()) {
				//选择的模块比目前数据库中保存的模块多，则在比对后进行添加
				for(String id : chk) {
					if(!contains(id, modules)) {
						PowerModule powerModule = new PowerModule();
						powerModule.setPowerId(Integer.parseInt(powerId));
						powerModule.setModuleId(Integer.parseInt(id));
						if(bo.addPowerModule(powerModule)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
			} else if(chk.length < modules.size()) {
				//选择的模块比目前数据库中保存的模块，则在比对后进行删除
				for(PowerModule item : modules) {
					if(!contains(item.getModuleId(), chk)) {
						if(bo.deletePowerModule(item)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
			} else {
				//选择的模块比目前数据库中保存的模块多，则在比对后进行添加
				for(String id : chk) {
					if(!contains(id, modules)) {
						PowerModule powerModule = new PowerModule();
						powerModule.setPowerId(Integer.parseInt(powerId));
						powerModule.setModuleId(Integer.parseInt(id));
						if(bo.addPowerModule(powerModule)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
				//选择的模块比目前数据库中保存的模块，则在比对后进行删除
				for(PowerModule item : modules) {
					if(!contains(item.getModuleId(), chk)) {
						if(bo.deletePowerModule(item)) {
							msg = "分配成功！";
						} else {
							msg = "分配失败，数据库错误！";
						}
					}
				}
			}
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.flush();
		out.write("<script>alert('" + msg + "');history.back(1);</script>");
		out.close();
		return null;
	}
	

	/**
	 * @param moduleId
	 * @param chks2
	 * @return
	 */
	private boolean contains(Integer moduleId, String[] chks) {
		for(String chk : chks) {
			if(Integer.parseInt(chk) == moduleId) {
				return true;
			}
		}
		return false;
	}
	/**
	 * @param moduleId
	 * @param modules
	 * @return
	 */
	private boolean contains(String moduleId, List<PowerModule> modules) {
		for(PowerModule item : modules) {
			if(moduleId.equals(item.getModuleId().toString())) {
				return true;
			}
		}
		return false;
	}
}
